package org.jeecg.modules.score.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.course.entity.AstuCourse;
import org.jeecg.modules.course.service.IAstuCourseService;
import org.jeecg.modules.experiment.entity.AstuExperiment;
import org.jeecg.modules.experiment.service.IAstuExperimentService;
import org.jeecg.modules.score.entity.AstuStudentScore;
import org.jeecg.modules.score.service.IAstuStudentScoreService;
import org.jeecg.modules.student.service.IAstuInformationService;
import org.jeecg.modules.tea_stu_course.entity.AstuTeastuCourse;
import org.jeecg.modules.tea_stu_course.service.IAstuTeastuCourseService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.vo.AstuStudentScoreVO;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 个人分数
 * @Author: jeecg-boot
 * @Date:   2021-09-25
 * @Version: V1.0
 */
@Api(tags="个人分数")
@RestController
@RequestMapping("/score/astuStudentScore")
@Slf4j
public class AstuStudentScoreController extends JeecgController<AstuStudentScore, IAstuStudentScoreService> {
	@Autowired
	private IAstuStudentScoreService astuStudentScoreService;
	@Autowired
	private IAstuInformationService astuinformationService;
	@Autowired
	private IAstuTeastuCourseService astuTeastuCourseService;
	@Autowired
	private IAstuCourseService astuCourseService;
	 @Autowired
	private IAstuExperimentService astuExperimentService;

	/**
	 * 分页列表查询
	 *
	 * @param astuStudentScore
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */

	//老师看自己课下学生的分数
	@AutoLog(value = "个人分数-分页列表查询")
	@ApiOperation(value="个人分数-分页列表查询", notes="个人分数-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AstuStudentScore astuStudentScore,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Subject subject = SecurityUtils.getSubject();
		LoginUser user = (LoginUser) subject.getPrincipal();
		List<AstuStudentScore> list = null;
		Page<AstuStudentScoreVO> page = new Page(pageNo, pageSize);
		List<AstuStudentScoreVO> list1 = new ArrayList<>();
		System.out.println("登录账号 相当于老师工号:"+user.getUsername());
		List<AstuTeastuCourse> list2 = astuTeastuCourseService.list(new QueryWrapper<AstuTeastuCourse>().eq("teacher_number", user.getUsername()));
		ArrayList<AstuStudentScore> astuStudentScores = new ArrayList<>();
		for (AstuTeastuCourse course : list2) {
			//学生学号
			String studentNumber = course.getStudentNumber();
			//课程号
			String courseId = course.getCourseId();
			//根据学号和课程号
			AstuStudentScore score = astuStudentScoreService.selectByStuNumberAndcourseId(studentNumber, courseId );
//       System.out.println("分数信息=====>"+score);
			if (score!=null){
				astuStudentScores.add(score);
			}
		}
		for (AstuStudentScore studentScore : astuStudentScores) {
			AstuStudentScoreVO studentScoreVO = new AstuStudentScoreVO();
			BeanUtils.copyProperties(studentScore,studentScoreVO);
			//学生学号
			String stuNum = studentScore.getStuNumber();
			//课程号
			String courseNum = studentScore.getCourseId();
			//实验号
			String experimentNum = studentScore.getExperimentId();
			//设置学生姓名
			String stuName = astuinformationService.selectStuNameByStuNum(stuNum);
			studentScoreVO.setStuName(stuName);
			//设置课程名
			LambdaQueryWrapper<AstuCourse> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq( AstuCourse::getCourseNumber,courseNum);
			AstuCourse one = astuCourseService.getOne(queryWrapper);
			String courseName = one.getCourseName();
			studentScoreVO.setCourseName(courseName);
			//设置实验名
			LambdaQueryWrapper<AstuExperiment> experimentWrapper = new LambdaQueryWrapper<>();
			experimentWrapper.eq(AstuExperiment::getExperimentSerialNumber, experimentNum);
			AstuExperiment one1 = astuExperimentService.getOne(experimentWrapper);
			String experimentName = one1.getExperimentName();
			studentScoreVO.setExperimentName(experimentName);
			list1.add(studentScoreVO);
		}
		page.setRecords(list1);
		return Result.OK(page);
	}

	//学生看自己的分数
	@AutoLog(value = "个人分数-分页列表查询")
	@ApiOperation(value="个人分数-分页列表查询", notes="个人分数-分页列表查询")
	@GetMapping(value = "/list2")
	public Result<?> queryPageList2(AstuStudentScore astuStudentScore,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//获取当前登录用户的信息
		Subject subject = SecurityUtils.getSubject();
		LoginUser user = (LoginUser) subject.getPrincipal();
		List<AstuStudentScore> list = null;
		Page<AstuStudentScoreVO> page = new Page(pageNo, pageSize);
		List<AstuStudentScoreVO> list1 = new ArrayList<>();
		if (user.getUsername().equalsIgnoreCase("admin")){
			 list = astuStudentScoreService.list();
		}else {
			 list = astuStudentScoreService.list(new QueryWrapper<AstuStudentScore>().eq("stu_number", user.getUsername()));
		}
		for (AstuStudentScore studentScore : list) {
			AstuStudentScoreVO studentScoreVO = new AstuStudentScoreVO();
			BeanUtils.copyProperties(studentScore,studentScoreVO);
			//学生学号
			String stuNum = studentScore.getStuNumber();
			//课程号
			String courseNum = studentScore.getCourseId();
			//实验号
			String experimentNum = studentScore.getExperimentId();
			//设置学生姓名
			String stuName = astuinformationService.selectStuNameByStuNum(stuNum);
			studentScoreVO.setStuName(stuName);
			//设置课程名
			LambdaQueryWrapper<AstuCourse> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq( AstuCourse::getCourseNumber,courseNum);
			AstuCourse one = astuCourseService.getOne(queryWrapper);
			String courseName = one.getCourseName();
			studentScoreVO.setCourseName(courseName);
			//设置实验名
			LambdaQueryWrapper<AstuExperiment> experimentWrapper = new LambdaQueryWrapper<>();
			experimentWrapper.eq(AstuExperiment::getExperimentSerialNumber, experimentNum);
			AstuExperiment one1 = astuExperimentService.getOne(experimentWrapper);
			String experimentName = one1.getExperimentName();
			studentScoreVO.setExperimentName(experimentName);
			list1.add(studentScoreVO);
		}
		page.setRecords(list1);
		return Result.OK(page);
	}

	/**
	 *   添加
	 *
	 * @param astuStudentScore
	 * @return
	 */
	@AutoLog(value = "个人分数-添加")
	@ApiOperation(value="个人分数-添加", notes="个人分数-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AstuStudentScore astuStudentScore) {
		astuStudentScoreService.save(astuStudentScore);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param astuStudentScore
	 * @return
	 */
	@AutoLog(value = "个人分数-编辑")
	@ApiOperation(value="个人分数-编辑", notes="个人分数-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AstuStudentScore astuStudentScore) {
		astuStudentScoreService.updateById(astuStudentScore);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人分数-通过id删除")
	@ApiOperation(value="个人分数-通过id删除", notes="个人分数-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		astuStudentScoreService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "个人分数-批量删除")
	@ApiOperation(value="个人分数-批量删除", notes="个人分数-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.astuStudentScoreService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人分数-通过id查询")
	@ApiOperation(value="个人分数-通过id查询", notes="个人分数-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AstuStudentScore astuStudentScore = astuStudentScoreService.getById(id);
		if(astuStudentScore==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(astuStudentScore);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param astuStudentScore
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AstuStudentScoreVO astuStudentScore) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		List<AstuStudentScore> list = astuStudentScoreService.list();
		ArrayList<AstuStudentScoreVO> list1 = new ArrayList<>();
		for (AstuStudentScore studentScore : list) {
			AstuStudentScoreVO astuStudentScoreVO = new AstuStudentScoreVO();
			BeanUtils.copyProperties(studentScore, astuStudentScoreVO);
			//学号
			String stuNumber = studentScore.getStuNumber();
			String stuName = astuinformationService.selectStuNameByStuNum(stuNumber);
			astuStudentScoreVO.setStuName(stuName);
			//课程号
			String courseId = studentScore.getCourseId();
			LambdaQueryWrapper<AstuCourse> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(AstuCourse::getCourseNumber, courseId);
			AstuCourse one = astuCourseService.getOne(queryWrapper);
			String courseName = one.getCourseName();
			astuStudentScoreVO.setCourseName(courseName);
			//实验号
			String experimentId = studentScore.getExperimentId();
			LambdaQueryWrapper<AstuExperiment> queryWrapper1 = new LambdaQueryWrapper<>();
			queryWrapper1.eq(AstuExperiment::getExperimentSerialNumber, experimentId);
			String experimentName = astuExperimentService.getOne(queryWrapper1).getExperimentName();
			astuStudentScoreVO.setExperimentName(experimentName);
			list1.add(astuStudentScoreVO);
		}
		mv.addObject(NormalExcelConstants.FILE_NAME, "学生分数");
		mv.addObject(NormalExcelConstants.CLASS, AstuStudentScoreVO.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生分数报表", "导出人:" + sysUser.getRealname(), "学生分数"));
		mv.addObject(NormalExcelConstants.DATA_LIST, list1);
		return mv;
}

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AstuStudentScore.class);
    }
}
